﻿using System;
using System.Collections.Generic;
using System.Text;
using FlickrNet;
using FlickrMe;
using System.IO;
using System.Net;
using FlickrMe.Upload;
using System.Windows.Forms;

namespace FlicrkrMe.Upload
{
    public delegate void UploadStart(int noFiles, long totalBytes);
    public delegate void UploadComplete();
    public delegate void FileUploadStart(FileInfo fileInfo);
    public delegate void FileUploadComplete();
    public delegate void FileUploadProgress(long bytesUploaded);

    public class FlickrUpload
    {

        Flickr flickr;
        private String[] filenames;
        private String currentFile;
        Boolean cancel = false;

        public event UploadStart UploadStartEvent;
        public event UploadComplete UploadCompleteEvent;
        public event FileUploadStart FileUploadStartEvent;
        public event FileUploadComplete FileUploadCompleteEvent;
        public event FileUploadProgress FileUploadProgressEvent;


      /*  public long GetTotalBytes()
        {
            return totalBytes;
        }*/

        public String CurrentFile
        {
            get { return currentFile; }
        }

        public String[] Filenames
        {
            get { return filenames; }
            set { filenames = value; }
        }

        public bool Cancel
        {
            set { value = true; }
            get { return cancel; }
        }


        public FlickrUpload(Flickr flickr)
        {
            this.flickr = flickr;
            
            this.UploadStartEvent += new UploadStart(FlickrUpload_UploadStartEvent);
            this.UploadCompleteEvent += new UploadComplete(FlickrUpload_UploadCompleteEvent);
            this.FileUploadStartEvent += new FileUploadStart(FlickrUpload_FileUploadStartEvent);
            this.FileUploadCompleteEvent += new FileUploadComplete(FlickrUpload_FileUploadCompleteEvent);
            this.FileUploadProgressEvent += new FileUploadProgress(FlickrUpload_FileUploadProgressEvent);
        }

        
        public void uploadFiles()  {

            UploadStartEvent(filenames.Length, Utility.getTotalFileByteSize(filenames));

            flickr.OnUploadProgress += new Flickr.UploadProgressHandler(updateProgress);
           
            // upload each file one at a time
            //
            foreach(String filename in filenames) 
            {
                if (cancel) break;
                
                FileUploadStartEvent(new FileInfo(filename));
                currentFile = filename;
                try
                {
                    flickr.UploadPicture(filename);
                }
                catch (WebException e)
                {
                    UploadCompleteEvent();
                    throw new UploadException(e.Message);
                }
                FileUploadCompleteEvent();
            }
            UploadCompleteEvent();
        }

        

        void updateProgress(Object obj, UploadProgressEventArgs args)
        {
            FileUploadProgressEvent(args.Bytes);
        }

        void FlickrUpload_UploadStartEvent(int noFiles, long totalBytes)
        { }

        void FlickrUpload_UploadCompleteEvent()
        { }

        void FlickrUpload_FileUploadStartEvent(FileInfo info)
        { }

        void FlickrUpload_FileUploadCompleteEvent()
        { }

        private void FlickrUpload_FileUploadProgressEvent(long bytesUploaded)
        { }
    }
}
